//
//  WYCForgetController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCForgetController.h"
#import "UIButton+CountDown.h"
#import "UIViewController+HUD.h"
#import "RSAUtil.h"
@interface WYCForgetController ()
@property(nonatomic,strong)UITextField * usertexfield;
@property(nonnull,strong) UITextField * codefield;
@property(nonatomic,strong)UIButton  *codebutton;
@property(nonatomic,strong) UITextField * passtexfield ;
@property(nonatomic,strong)UITextField * Zpasstexfield;
@end

@implementation WYCForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建界面
    [self setupCreat];
}

#pragma make ------>>界面创建
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = wycMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 40 * px, 300 * px, 64);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"忘记密码";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(40 * px, NavRigsterLabel.height / 3, 40 , 40);
    [registerView addSubview:backbutton];
    
     _usertexfield = [[UITextField alloc] init];
    _usertexfield.frame = CGRectMake(80 * px,CGRectGetMaxY(registerView.frame) + 80 * px, ScreenWidth - WYCcellHeight, 120 * px);
    _usertexfield.backgroundColor = WYCRGBColor(249,249, 249);
    _usertexfield.placeholder = @"请输入手机号";
    _usertexfield.tintColor = [UIColor redColor];

    [self.view addSubview:_usertexfield];
    UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView.backgroundColor = [UIColor clearColor];
    _usertexfield.leftView = leftView;
    _usertexfield.leftViewMode = UITextFieldViewModeAlways;
    _usertexfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //手机提示线
    UIView * lineView = [[UIView alloc] init];
   lineView.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(_usertexfield.frame) + 5 * px, _usertexfield.width, 1);
//    lineView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:lineView];
    
    
    ////////////////
    /////密码输入框/////
    ///////////////
    
    
    
    
    _codefield= [[UITextField alloc] init];
    _codefield.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(lineView.frame) + 80 * px, _usertexfield.width, _usertexfield.height );
    _codefield.backgroundColor = WYCRGBColor(249,249, 249);
    _codefield.placeholder = @"验证码";
    _codefield.tintColor = [UIColor redColor];
    [self.view addSubview:_codefield];
    UILabel * leftView1 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView1.backgroundColor = [UIColor clearColor];
    _codefield.leftView = leftView1;
    _codefield.leftViewMode = UITextFieldViewModeAlways;
    _codefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    //手机提示线
    UIView * passlineView = [[UIView alloc] init];
    passlineView.frame = CGRectMake(_codefield.x, CGRectGetMaxY(_codefield.frame) + 5 * px, lineView.width, 1);
    [self.view addSubview:passlineView];
    
    //获取验证码
     _codebutton = [[UIButton alloc] init];
    _codebutton.frame = CGRectMake(ScreenWidth - 370 * px, _codefield.y, 300 * px, _codefield.height);
    [_codebutton setTitleColor:wycMain forState:UIControlStateNormal];
    _codebutton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [_codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codebutton];
    
    
    _passtexfield = [[UITextField alloc] init];
    _passtexfield.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(passlineView.frame) + 80 * px, _usertexfield.width, _usertexfield.height);
    _passtexfield.placeholder = @"密码";
    _passtexfield.tintColor = [UIColor redColor];
    _passtexfield.backgroundColor = WYCRGBColor(249,249, 249);
    _passtexfield.secureTextEntry = YES;
    [self.view addSubview:_passtexfield];
    UILabel * leftView2 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView2.backgroundColor = [UIColor clearColor];
    _passtexfield.leftView = leftView2;
    _passtexfield.leftViewMode = UITextFieldViewModeAlways;
    _passtexfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //手机提示线
    UIView * passView = [[UIView alloc] init];
    passView.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(_passtexfield.frame) + 5 * px, _usertexfield.width, 1);
//    passView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passView];
    
    
    
    
    /************再次输入密码************/
    
    
    _Zpasstexfield     = [[UITextField alloc] init];
    _Zpasstexfield.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(passView.frame)+80 * px, _usertexfield.width, _usertexfield.height);
    _Zpasstexfield.placeholder = @"再次输入密码";
    _Zpasstexfield.secureTextEntry = YES;
    _Zpasstexfield.tintColor = [UIColor redColor];
    _Zpasstexfield.backgroundColor = WYCRGBColor(249,249, 249);
    [self.view addSubview:_Zpasstexfield];
    UILabel * leftView3 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView3.backgroundColor = [UIColor clearColor];
    _Zpasstexfield.leftView = leftView3;
    _Zpasstexfield.leftViewMode = UITextFieldViewModeAlways;
    _Zpasstexfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //手机提示线
    UIView * ZpassView = [[UIView alloc] init];
    ZpassView.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(_Zpasstexfield.frame) + 5 * px, _usertexfield.width, 1);
//    ZpassView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:ZpassView];
    
    
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(_usertexfield.x, CGRectGetMaxY(ZpassView.frame) + 300 * px, _usertexfield.width, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"立即找回" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(findBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];    
    
}


#pragma make --------->>找回
- (void)findBtn{
    WYCLogFunc;
    
    // 手机号判断是否为空
    if(_usertexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        return;
    }
    //验证码判断是否为空
    if(_codefield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
        return;
    }
    //密码是否为空
    if(_passtexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    //密码判断是否为空
    
    if(self.passtexfield.text != self.Zpasstexfield.text)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"两次输入密码不一致"];
        return;
    }
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
    NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    //
    
    params[@"ticket"] = [WYCAccountTool unarchiveticket];
    params[@"authcode"] = _codefield.text;
    params[@"newPwd"] = pas;
    
    WYCLog(@"--------%@",params);
    [HttpTool POST:safemodifyPasswordByForget params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        WYCLog(@"找回密码%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [self showToast:@"找回成功"];
            [self backClick];
        }else{
            [self showToast:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
}
#pragma make -------->>返回
- (void)backClick{
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}
#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
if (_usertexfield.text.length == 0) {
    NSString * showStr = @"手机号不能为空";
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
    return;
}else{
    [_codebutton startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    
    NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,_usertexfield.text,Ucodekey];
    
    [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
 }
}
@end
